<?php
	/**
 	 * Models controller class
 	 */
	class ModelsController extends Controller
	{
		/**
	 	 * page title
	 	 */
		public $page_title = LANG_MODELS;
		
		/**
	 	 * page layout
	 	 */
		public $layout = 'main';
		
		/**
	 	 * necessary helpers
	 	 */
		public $helpers = array ('html', 'htmlx');
		
		/**
	 	 * necessary javascript files
	 	 */
		public $javascript = array("TreeMenu");
		
		/**
	 	 * necessary components
	 	 */
		public $components = array ("TreeMenu");
		
		/**
	 	 * adds a page page
	 	 */
		function add()
		{
			//model features
			$this->model_features_string = array('name', 'type', 'sub_type', 'table_name', 'length', 'min_value', 'max_value', 'default_value', 'by_default');
			$this->model_features_boolean = array('primary_key', 'not_null');
			
			//types, subtypes
			$this->field_types = array
			(
				'1' => array
				(
					'id' => 'bigint', 
					'name' => LANG_BIGINT
				),
				'2' => array
				(
					'id' => 'varchar', 
					'name' => LANG_VARCHAR
				),
				'3' => array
				(
					'id' => 'text', 
					'name' => LANG_TEXT
				),
				'4' => array
				(
					'id' => 'decimal (16,2)', 
					'name' => LANG_DECIMAL
				)
			);
			
			$this->field_subtypes = array
			(
				'1' => array
				(
					'id' => 'percent', 
					'name' => LANG_PERCENT
				),
				'2' => array
				(
					'id' => 'email', 
					'name' => LANG_EMAIL
				),
				'3' => array
				(
					'id' => 'url', 
					'name' => LANG_URL
				)
			);
			
			//if we posted smth
			if (!empty($this->request->post))
			{
				//creates the file with the specified name
				$fp = fopen('app'.SEPARATOR.'models'.SEPARATOR.$this->request->post[name].'.php', 'w+');
				
				//file beginning
				$start_file = "<?php \n\tClass ".ucfirst($this->request->post[name])." extends Model \n\t{\n";
				
				//class variables
				$variables = "\t\t".'public $database_config = \''.$this->request->post[database_config]."';\n";
				$variables .= "\t\t".'public $name = \''.$this->request->post[name]."';\n";
				$variables .= "\t\t".'public $table_name = \''.$this->request->post[table_name]."';\n";
				$variables .= "\t\t".'public $stored_procedure_list = \''.$this->request->post[stored_procedure_list]."';\n";
				$variables .= "\t\t".'public $stored_procedure_list_paged = \''.$this->request->post[stored_procedure_list_paged]."';\n";
				$variables .= "\t\t".'public $stored_procedure_update = \''.$this->request->post[stored_procedure_update]."';\n";
				$variables .= "\t\t".'public $stored_procedure_get = \''.$this->request->post[stored_procedure_get]."';\n";
				$variables .= "\t\t".'public $stored_procedure_delete = \''.$this->request->post[stored_procedure_delete]."';\n";
				$variables .= "\t\t".'public $stored_procedure_insert = \''.$this->request->post[stored_procedure_insert]."';\n";
				
				//db structure beginning
				$start_db_structure = "\n\t\t".'public $db_structure = array'."\n\t\t(\n";
				
				//db structure
				$db_structure = '';
				
				//db structure generation
				foreach ($this->request->post[elements] as $element)
				{
					if (empty($element[app_name])) continue;
					
					$db_structure .= "\t\t\t'{$element[app_name]}' => array\n\t\t\t(\n";
					
					//string variables
					foreach ($this->model_features_string as $feature)
					{
						if ($element[$feature]!='')
							$db_structure .= "\t\t\t\t'$feature' => '{$element[$feature]}',\n";
					}
					
					//boolean variables
					foreach ($this->model_features_boolean as $feature)
					{
						if ($element[$feature]=='1') $value = 'true';
						else $value = 'false';
						$db_structure .= "\t\t\t\t'$feature' => '$value',\n";
					}
					
					$db_structure .= "\t\t\t),\n";
				}
								
				//db structure ending
				$end_db_structure = "\t\t);";
				
				//file ending
				$end_file = "\n\t}\n?>";
				
				//file content
				$content = $start_file.$variables.$start_db_structure.$db_structure.$end_db_structure.$end_file;
				
				//writes to the file
				fwrite($fp, $content);
				
				//closes the file
				fclose($fp);
			}			
		}
	}
?>